package util;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.net.URL;

/**
 * Created by zhongwei on 2016/12/24.
 */
public class WebDriverFactory {

    private static WebDriver driver;

    public static WebDriver getWebDriver(String[] args) {
        // 判断操作系统
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.startsWith("windows")) {
            System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
//            System.setProperty("webdriver.firefox.bin", "D:\\Mozilla Firefox\\firefox.exe");
            URL url = ClassLoader.getSystemResource("gecko/geckodriver.exe");
            System.setProperty("webdriver.gecko.driver", url.getPath());
            try {
                driver = new FirefoxDriver();
            } catch (RuntimeException ex) {
                System.out.println("Usage:\njava -jar weixin-XXX.jar \"firefox.exe或者firefox-bin的位置\"");
                System.exit(-1);
            }
        }
        if (osName.startsWith("mac")) {
            System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox-bin");
            URL url = ClassLoader.getSystemResource("gecko/geckodriver");
            System.setProperty("webdriver.gecko.driver", url.getPath());
            try {
                driver = new ChromeDriver();
            } catch (RuntimeException ex) {
                System.out.println("Usage:\njava -jar weixin-XXX.jar \"firefox.exe或者firefox-bin的位置\"");
                System.exit(-1);
            }
        }
        // 如果路径还不对，让用户指定
        return driver;
    }
}
